package com.my.mymafengwo.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.smallstrong.mafengwopro.R;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.my.mymafengwo.activity.MoreResultActivity;
import com.my.mymafengwo.activity.SearchActivity;
import com.my.mymafengwo.activity.SearchStrategyActivity;
import com.my.mymafengwo.activity.StoreActivity;
import com.my.mymafengwo.activity.TravelNotesActivity;
import com.my.mymafengwo.adapter.BaseAdapterHelper;
import com.my.mymafengwo.adapter.QuickAdapter;
import com.my.mymafengwo.base.BaseFragment;
import com.my.mymafengwo.bean.CommonSquares;
import com.my.mymafengwo.bean.EliteNote;
import com.my.mymafengwo.bean.MainIcon;
import com.my.mymafengwo.bean.Review;
import com.my.mymafengwo.bean.SalesMdds;
import com.my.mymafengwo.utils.CircleTransformUtils;
import com.my.mymafengwo.utils.UrlUtils;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * ***********************************************
 * author:wgh
 * time:2015/12/3 22:18
 * copyright:
 * name: 发现
 * step:
 * ***********************************************
 */
public class DiscoverFragment extends BaseFragment implements View.OnClickListener {

    /**
     * MainIcon集合
     */
    List<MainIcon.DataEntity> listMainIcon;
    /**
     * SalesMdds集合
     */
    List<SalesMdds.DataEntity.ListEntity> listSalesMdds;
    /**
     * CommonSquares集合
     */
    List<CommonSquares.DataEntity.ListEntity> listCommonSquares;

    /**
     * 第一节界面的
     * 六个子菜单的适配器
     * 正在热卖的适配器
     * 目的地推荐的适配器
     *
     * @return
     */
    QuickAdapter<MainIcon.DataEntity> adapter;
    QuickAdapter<SalesMdds.DataEntity.ListEntity> adapterSalesmdds;
    QuickAdapter<CommonSquares.DataEntity.ListEntity> adapterCommonSquares;

    private JSONArray list;
    private Review review;
    private MainIcon mainIcon;

    /**
     * 峰首对象
     */
    private EliteNote eliteNote;
    /**
     * 热卖对象
     */
    private SalesMdds salesMdds;
    /**
     * 目的地推荐对象
     */
    private CommonSquares commonSquares;
    /**
     * 第一节界面的六个子菜单的GridView对象
     */
    private GridView gvMainIcon, gvSalesMdds, gvCommonSquares;

    /**
     * 今日蜂首的大图片和小图片和底部的往期回顾图片对象
     */
    private ImageView ivBig, ivSmall, ivReview, ivOldElite;
    /**
     * 今日蜂首下边的文字对象
     */
    private TextView tvWho, tvWhere, tvDo, tvCommonSquares, tvEliteNote, tvSalesMdds,
            tvOldElite, tvHotBuy, tvSeeMore;
    private EditText etSearch;
    private RelativeLayout rlayout;

    @Override
    protected View initView() {
        View v = View.inflate(getActivity(), R.layout.fragment_discover, null);
        gvMainIcon = (GridView) v.findViewById(R.id.gridview_first);
        gvSalesMdds = (GridView) v.findViewById(R.id.gridview_second);
        gvCommonSquares = (GridView) v.findViewById(R.id.gridview_third);
        ivBig = (ImageView) v.findViewById(R.id.iv_big);
        ivSmall = (ImageView) v.findViewById(R.id.iv_small);
        ivOldElite = (ImageView) v.findViewById(R.id.iv_old_elite);
        ivReview = (ImageView) v.findViewById(R.id.iv_back_see);
        tvWho = (TextView) v.findViewById(R.id.tv_who);
        tvWhere = (TextView) v.findViewById(R.id.tv_where);
        tvDo = (TextView) v.findViewById(R.id.tv_do);
        tvCommonSquares = (TextView) v.findViewById(R.id.sub_title_url_common);
        tvEliteNote = (TextView) v.findViewById(R.id.sub_title_url_elite);
        tvSalesMdds = (TextView) v.findViewById(R.id.sub_title_url_sales);
        tvOldElite = (TextView) v.findViewById(R.id.tv_old_elite);
        tvHotBuy = (TextView) v.findViewById(R.id.tv_hot_buy);
        tvSeeMore = (TextView) v.findViewById(R.id.tv_see_more);
        rlayout = (RelativeLayout) v.findViewById(R.id.rl_first);
        etSearch = (EditText) v.findViewById(R.id.et_search);
        return v;
    }

    @Override
    protected void initData() {
        loadData();
        registerListener();
    }

    /**
     * 设置监听
     */
    private void registerListener() {
        tvOldElite.setOnClickListener(this);
        tvHotBuy.setOnClickListener(this);
        tvSeeMore.setOnClickListener(this);
        ivOldElite.setOnClickListener(this);
        etSearch.setOnClickListener(this);
    }

    /**
     * 下载第一面的所有数据 并显示
     */
    private void loadData() {
        final HttpUtils http = new HttpUtils();

        http.configDefaultHttpCacheExpiry(100 * 1024);
        http.send(HttpRequest.HttpMethod.GET, UrlUtils.MAIN_URL, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                if (result != null) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        JSONObject data = jsonObject.getJSONObject("data");
                        list = data.getJSONArray("list");
                        Gson gson = new Gson();
                        mainIcon = gson.fromJson(list.get(0).toString(), MainIcon.class);
                        eliteNote = gson.fromJson(list.get(1).toString(), EliteNote.class);
                        salesMdds = gson.fromJson(list.get(2).toString(), SalesMdds.class);
                        commonSquares = gson.fromJson(list.get(3).toString(), CommonSquares.class);
                        mainIconIn();
                        eliteNoteIn();
                        salesMddsIn();
                        commonSquaresIn();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
        /**
         * 加载底部往期回顾图片，并显示
         */
        http.send(HttpRequest.HttpMethod.GET, UrlUtils.MAIN_REVIEW_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                if (result != null) {
                    try {
                        Gson gson = new Gson();
                        review = gson.fromJson(result, Review.class);
                        Picasso.with(mActivity).load(review.getData().getList().get(0).getImg_url()).into(ivReview);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
            }
        });
        /**
         * 往期回顾跳转页面
         */
        ivReview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, TravelNotesActivity.class);
                intent.putExtra("url", UrlUtils.MAIN_REVIEW_URL_JUMP);
                startActivity(intent);
            }
        });
    }

    /**
     * 目的地推荐的图片下载和填入
     */
    private void commonSquaresIn() {
        listCommonSquares = commonSquares.getData().getList();
        adapterCommonSquares = new QuickAdapter<CommonSquares.DataEntity.ListEntity>(mActivity, R.layout.list_item_commonsquares, listCommonSquares) {
            @Override
            protected void convert(BaseAdapterHelper helper, CommonSquares.DataEntity.ListEntity item) {
                helper.setText(R.id.tv_month, item.getTitle());
                helper.setText(R.id.tv_sub_title, item.getSub_title());
                helper.setImageUrl(R.id.iv_commonsquares, item.getImg_url());
            }
        };
        gvCommonSquares.setAdapter(adapterCommonSquares);
        gvCommonSquares.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mActivity, TravelNotesActivity.class);
                intent.putExtra("url", listCommonSquares.get(position).getJump_url());
                startActivity(intent);
            }
        });
        tvCommonSquares.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, TravelNotesActivity.class);
                intent.putExtra("url", commonSquares.getData().getSub_title_url());
                startActivity(intent);
            }
        });
    }

    /**
     * 正在热卖图片下载及填入
     */
    private void salesMddsIn() {
        listSalesMdds = salesMdds.getData().getList();
        adapterSalesmdds = new QuickAdapter<SalesMdds.DataEntity.ListEntity>(mActivity, R.layout.list_item_salesmdds, listSalesMdds) {

            @Override
            protected void convert(BaseAdapterHelper helper, SalesMdds.DataEntity.ListEntity item) {
                helper.setText(R.id.tv_salesmdds_where, item.getTitle());
                helper.setText(R.id.tv_salesmdds_time, item.getSub_title());
                helper.setText(R.id.tv_salesmdds_money, item.getPrice());
                helper.setImageUrl(R.id.iv_salesmdds, item.getThumbnail());
            }
        };
        gvSalesMdds.setAdapter(adapterSalesmdds);

        gvSalesMdds.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mActivity, TravelNotesActivity.class);
                intent.putExtra("url", listSalesMdds.get(position).getJump_url());
                startActivity(intent);
            }
        });
        tvSalesMdds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, TravelNotesActivity.class);
                intent.putExtra("url", salesMdds.getData().getSub_title_url());
                startActivity(intent);
            }
        });
    }

    /**
     * 今日蜂首的图片下载和字体填充
     */
    private void eliteNoteIn() {
        Picasso.with(mActivity).load(eliteNote.getData().getNote().getThumbnail()).into(ivBig);
        Picasso.with(mActivity).load(eliteNote.getData().getNote().getUser().getLogo()).transform(new CircleTransformUtils()).into(ivSmall);

        tvWho.setText(eliteNote.getData().getNote().getUser().getName()+" 在 ");
        tvWhere.setText(eliteNote.getData().getNote().getMdds().get(0).getName());
        tvDo.setText(eliteNote.getData().getNote().getTitle());

        rlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpEliteNote();
            }
        });
        tvEliteNote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpEliteNote();
            }
        });
    }

    /**
     * 今日蜂首内的包括图片文字的点击跳转
     */
    private void jumpEliteNote() {
        Intent intent = new Intent(mActivity, TravelNotesActivity.class);
        intent.putExtra("url", eliteNote.getData().getNote().getJump_url());
        startActivity(intent);
    }

    /**
     * 主界面上面的六个小菜单
     */
    private void mainIconIn() {
        listMainIcon = mainIcon.getData();
        adapter = new QuickAdapter<MainIcon.DataEntity>(mActivity, R.layout.list_item_of_main_icon, listMainIcon) {
            @Override
            protected void convert(BaseAdapterHelper helper, MainIcon.DataEntity item) {
                helper.setText(R.id.ItemText, item.getTitle());
                helper.setImageUrl(R.id.ItemImage, item.getIcon());
            }
        };
        gvMainIcon.setAdapter(adapter);
        gvMainIcon.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        Intent intent = new Intent(mActivity, SearchStrategyActivity.class);
                        startActivity(intent);
                        break;
                    case 2:
                        Intent intent2 = new Intent(mActivity, StoreActivity.class);
                        startActivity(intent2);
                        break;
                }

            }
        });
    }

    /**
     * 点击跳转
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_old_elite://往期蜂首
                jumpActivity("往期蜂首");
                break;
            case R.id.iv_old_elite://往期蜂首边的图片
                jumpActivity("往期蜂首");
                break;
            case R.id.tv_hot_buy://更多超值热卖
                jumpActivity("正在热卖");
                break;
            case R.id.tv_see_more://查看更多
                jumpActivity("全年当季推荐");
                break;
            case R.id.et_search://搜索框点击跳转
                startActivity(new Intent(mActivity, SearchActivity.class));
                break;

        }
    }

    /**
     * 跳转到 MoreResultActivity 页面并传递数据
     *
     * @param name
     */
    public void jumpActivity(String name) {
        Intent intent = new Intent(mActivity, MoreResultActivity.class);
        intent.putExtra("str", name);
        startActivity(intent);
    }
}
